'''
Created on 01/05/2011

@author: Adam
'''
from flask import render_template, flash

from application.models import Mappers, Map
from application.decorators import admin_required, template_init
from application.forms.importer import ImporterForm

from google.appengine.ext.deferred import defer

@admin_required
@template_init
def importer():
	form = ImporterForm()
	form.regionMoons.choices = [(str(r.key()), r.name) for r in Map.Region.all().order('name')]
	if form.validate_on_submit():
		for option in form.dataOptions.data:
			if option == 'map':
				flash(u'Import of Map data has been started')
				mapper = Mappers.ImportRegionsMapper(form.dataSourceUrl.data, queue='importer')
				defer(mapper.runMapper, _queue='importer')
			elif option == 'modules':
				flash(u'Import of POS Module data has not been implemented')
			elif option == 'towers':
				flash(u'Import of Towers has been started')
				mapper = Mappers.ImportTowerFuelTypeMapper(form.dataSourceUrl.data, queue='importer')
				defer(mapper.runMapper, _queue='importer')
		for regionKey in form.regionMoons.data:
			region = Map.Region.get(regionKey)
			flash(u'Import of moons in %s has been started' % region.name)
			Mappers.ImportMoonsMapper.runAllMappers(form.dataSourceUrl.data, region)
	return render_template('importer/importForm.html', form=form)